// pages/operation/points/pointsRule/index.js
import formRules from "../../../../utils/rules-form.js";
import { Points } from "../../../../api/api-work/points.js";
const points = new Points();
import Toast from "@vant/weapp/toast/toast";
const rules = {
  name: [
    {
      required: true,
      message: "请输入积分名称",
    },
  ],
};
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: "",
    name: "",
    attr1: "",
    attr2: "",
    attr3: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},
  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    // this.setData({
    //   pageNum: 1,
    //   tableDatas: []
    // })
    await this.getPointsRule();
  },
  async getPointsRule() {
    points.getPointsRuleAPI({}).then((res) => {
      this.setData({
        id: res.id,
        name: res.name,
        attr1: res.attr1,
        attr2: res.attr2,
        attr3: res.attr3,
      });
    });
  },

  onChange1(e) {
    this.setData({
      name: e.detail,
    });
  },
  onChange2(e) {
    this.setData({
      attr1: e.detail,
    });
  },
  onChange3(e) {
    this.setData({
      attr2: e.detail,
    });
  },
  onChange4(e) {
    this.setData({
      attr3: e.detail,
    });
  },
  async savePointsRule() {
    const params = {
      id: this.data.id,
      name: this.data.name,
      attr1: this.data.attr1,
      attr2: this.data.attr2,
      attr3: this.data.attr3,
    };
    formRules(rules, this.data, (status) => {
      if (status) {
        points.savePointsRuleAPI(params).then((res) => {
          Toast("规则设置保存成功");
          this.getPointsRule();
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
Component({
  data: {
    id: "",
    name: "",
    attr1: "",
    attr2: "",
    attr3: "",
  },

  created() {
    this.getPointsRule();
  },
  methods: {
    async getPointsRule() {
      points.getPointsRuleAPI({}).then((res) => {
        this.setData({
          id: res.id,
          name: res.name,
          attr1: res.attr1,
          attr2: res.attr2,
          attr3: res.attr3,
        });
      });
    },

    onChange1(e) {
      this.setData({
        name: e.detail,
      });
    },
    onChange2(e) {
      this.setData({
        attr1: e.detail,
      });
    },
    onChange3(e) {
      this.setData({
        attr2: e.detail,
      });
    },
    onChange4(e) {
      this.setData({
        attr3: e.detail,
      });
    },
    async savePointsRule() {
      const params = {
        id: this.data.id,
        name: this.data.name,
        attr1: this.data.attr1,
        attr2: this.data.attr2,
        attr3: this.data.attr3,
      };
      formRules(rules, this.data, (status) => {
        if (status) {
          points.savePointsRuleAPI(params).then((res) => {
            Toast("规则设置保存成功");
            this.getPointsRule();
          });
        }
      });
    },
  },
});
